package com.gitee.feizns.explore.data_structure.linked_list.singly.copyRandomList;

import com.gitee.feizns.explore.data_structure.linked_list.singly.nextLargerNodes.ListNode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author feizns
 * @since 2020/3/6
 */
public class CopyRandomList {

    public static void main(String[] args) {

    }

    public Node copyRandomList(Node head) {
        Map<Node, Node> map = new HashMap<>();
        for (Node curt = head; curt != null; curt = curt.next)
            map.put(curt, new Node(curt.val));
        for (Node curt = head; curt != null; curt = curt.next) {
            Node node = map.get(curt);
            node.next = map.get(curt.next);
            node.random = map.get(curt.random);
        }
        return map.get(head);
    }

}

class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
